<?php

namespace App\Http\Controllers;

use App\Models\AbstractUser;
use App\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 当前Guard
     *
     * @var string
     */
    protected $guard = 'web';


    /**
     * 跳转页面
     *
     * @param null $to
     * @param int $status
     * @param array $headers
     * @param null $secure
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    protected function redirect($to = null, $status = 302, $headers = [], $secure = null)
    {
        return redirect($to, $status, $headers, $secure);
    }

    /**
     * 跳转到指定路由
     *
     * @param $route
     * @param array $parameters
     * @param int $status
     * @param array $headers
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function route($route, $parameters = [], $status = 302, $headers = [])
    {
        return $this->redirect()->route($route, $parameters, $status, $headers);
    }

    /**
     * 返回到上一个页面
     *
     * @param int $status
     * @param array $headers
     * @param bool $fallback
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function back($status = 302, $headers = [], $fallback = false)
    {
        return $this->redirect()->back($status, $headers, $fallback);
    }

    /**
     * 跳转到登录前页面
     *
     * @param string $default
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    protected function intended($default = '/')
    {
        $redirect = Request::input('redirect');

        // 优先跳转到指定页面
        if ($redirect) {
            return $this->redirect($redirect);
        }

        return $this->redirect()->intended($default);
    }

    /**
     * 关闭窗口
     *
     * @param int $timeout
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function close($timeout = 0)
    {
        return $this->view('globals.close-window', compact('timeout'));
    }

    /**
     * 响应视图
     *
     * @param null $view
     * @param array $data
     * @param array $mergeData
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function view($view = null, $data = [], $mergeData = [])
    {
        return view($view, $data, $mergeData);
    }

    /**
     * 抛出HTTP异常
     *
     * @param $code
     * @param string $message
     * @param array $headers
     */
    protected function abort($code, $message = '', array $headers = [])
    {
        abort($code, $message, $headers);
    }

    /**
     * 抛出404异常
     *
     * @param string $message
     * @param array $headers
     */
    protected function abort404($message = '', array $headers = [])
    {
        $this->abort(404, $message, $headers);
    }

    /**
     * 返回自定义响应实例
     *
     * @param string $content
     * @param int $status
     * @param array $headers
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|Response
     */
    protected function response($content = '', $status = 200, array $headers = [])
    {
        return response($content, $status, $headers);
    }

    /**
     * 抛出一个可响应的异常
     *
     * @param Response $response
     */
    protected function responseException(Response $response)
    {
        throw new HttpResponseException($response);
    }

    /**
     * 抛出验证异常
     *
     * @param array $message
     *
     * @throws ValidationException
     */
    protected function validationException(array $message)
    {
        throw ValidationException::withMessages($message);
    }

    /**
     * 获取Guard
     *
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
     */
    protected function guard()
    {
        return Auth::guard($this->guard);
    }

    /**
     * 获取登录用户
     *
     * @return Authenticatable|User|AbstractUser|null
     */
    protected function user()
    {
        return $this->guard()->user();
    }

    /**
     * 是否登录
     *
     * @return bool
     */
    protected function check()
    {
        return $this->guard()->check();
    }
}
